package hello;

import org.hibernate.Session;
import org.hibernate.Transaction;

import persistence.HibernateUtil;
import auction.chapter08.ItemFKToCompPK;
import auction.chapter08.UserFKToCompPK;

public class HelloWorldFKToCompPK
{
	
	
	public static void main(String[] args)throws Exception
	{
		
		Session s=HibernateUtil.getSessionFactory().openSession();
		Transaction tx=s.beginTransaction();
		
		
		UserFKToCompPK u=new UserFKToCompPK();
		u.setUserName("wei");
		u.setDepartmentNr("avetti");
		
		ItemFKToCompPK i=new ItemFKToCompPK();
		i.setTitle("ttt");
		/*
		u.addItem(i);
		s.save(i);
		s.saveOrUpdate(u);
		*/
		tx.commit();
		s.close();
		
		
		
		Session s1=HibernateUtil.getSessionFactory().openSession();
		Transaction tx1=s1.beginTransaction();
		
		UserFKToCompPK key=new UserFKToCompPK();
		key.setUserName("wei");
		key.setDepartmentNr("avetti");
		
		UserFKToCompPK loaded=(UserFKToCompPK)s1.load(UserFKToCompPK.class, key);
		out(loaded.getItems().size());
		tx1.commit();
		s1.close();
		
		
		
		//HibernateUtil.stat();
		HibernateUtil.shutdown();
	}
	
	public static void getParent(Class clazz)
	{
		Class parent=clazz.getSuperclass();
	  if(parent !=null)
	  {
			System.out.println("###"+parent.getName());
			getParent(parent);
		}
	}
	
	static void out(Object obj)
	{
		System.out.println("---->"+obj);
	}
}
